<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Archivos {

    protected $CI;  

    function Archivos() {
        $this->CI = & get_instance();
    }

    public function upl_avatar(){
        $config['upload_path'] = AVATAR;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '3000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['file_name'] = $this->CI->session->userdata('usu_id')."_".strtolower($this->CI->session->userdata('usu_nick'))."_avatar";
        $config['overwrite'] = true;
        $this->CI->load->library('upload', $config);

        if ($this->CI->upload->do_upload('usu_avatar')) {
            /* Resizing if its required */
            $data_upload = $this->CI->upload->data();
            $path = AVATAR.$data_upload['file_name'];
            $this->_resize_image($path, 300, 200, false);
            /* --------------------------------------- */
            return $data_upload['file_name'];
        }
        return null;
    }
    
    public function upl_empresa($idempresa, $nombre){
        if (!file_exists(EMPRESAS.$idempresa)) {
            mkdir(EMPRESAS.$idempresa, 0700);
        }
        
        $config['upload_path'] = EMPRESAS.$idempresa."/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '3000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['file_name'] = $nombre;
        $this->CI->load->library('upload', $config);
        
        $return = array();
        if ($this->CI->upload->do_upload('emi_url')) {
            /* Resizing if its required */
            $data_upload = $this->CI->upload->data();
            $path = EMPRESAS.$idempresa."/".$data_upload['file_name'];
            $this->_resize_image($path, 300, 200, false, array('create_thumb' => true));
            /* --------------------------------------- */
            $return['url'] = $data_upload['file_name'];
            $return['url_thumb'] = $data_upload['raw_name']."_thumb".$data_upload['file_ext'];
        }
        if(count($return) > 0){
            return $return;
        }
        return null;
    }
    
    public function upl_producto($query){
        if (!file_exists(PRODUCTOS.$query['pro_id'])) {
            mkdir(PRODUCTOS.$query['pro_id'], 0700);
        }
        
        $config['upload_path'] = PRODUCTOS.$query['pro_id']."/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '3000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['file_name'] = $query['pro_slug']."-".count($query['d_imagenes']);
        $this->CI->load->library('upload', $config);
        
        $return = array();
        if ($this->CI->upload->do_upload('emi_url')) {
            /* Resizing if its required */
            $data_upload = $this->CI->upload->data();
            $return['url'] = $data_upload['file_name'];
            $return['url_thumb'] = $data_upload['raw_name']."_thumb".$data_upload['file_ext'];
            
            $path = PRODUCTOS.$query['pro_id']."/".$return['url'];
            $path_thumb = PRODUCTOS.$query['pro_id']."/".$return['url_thumb'];
            
            $this->_resize_image($path, 640, 480, false);
            $this->_resize_image($path, 300, 200, false, array('new_image' => $path_thumb));
            //$this->_resize_image($path_thumb, 300, 200, false, array('create_thumb' => true));
            /* --------------------------------------- */
        }
        if(count($return) > 0){
            return $return;
        }
        return null;
    }
    
    private function _resize_image($path, $width = 640, $height = 480, $watermark = true, $config = array()){
        $this->CI->load->library('image_lib');    
        $config['image_library'] = 'gd2';
        $config['source_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
        $config['height'] = $height;
        $config['master_dim'] = 'width';
        $this->CI->image_lib->initialize($config);
        $this->CI->image_lib->resize();
        $this->CI->image_lib->clear();
        if($watermark){
            $config["manipulation"]['source_image'] = $path;
            $config["manipulation"]['wm_text'] = 'Copyright '.date('Y').' - '.$this->CI->config->item('name_web').'.com';
            $config["manipulation"]['wm_type'] = 'text';
            $config["manipulation"]['wm_font_color'] = 'ffffff';
            $config["manipulation"]['wm_vrt_alignment'] = 'M';
            $this->CI->image_lib->initialize($config["manipulation"]);
            $this->CI->image_lib->watermark();
        }
    }
}

?>